package com.example.demo01;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

@RequestMapping("/rest")
@RestController
public class TemplateController {
    @Autowired
    private RestTemplate template;

    @GetMapping("/httpTest")
    public Object httpTest() {
        String url = "http://127.0.0.1:8081/test/httpTest";
        Map<String, Object> result = template.getForObject(url, Map.class);
        String msg = result.get("msg").toString();
        byte[] bytes = result.get("bytes").toString().getBytes();

        JSONObject jsonObject = template.getForObject(url, JSONObject.class);
        byte[] bytes1 = jsonObject.getBytes("bytes");
        String s = new String(bytes);
        String s1 = new String(bytes1);

        File file = new File("C:\\Users\\wangkl\\Desktop\\ceshi.txt");
        try (FileOutputStream fos = new FileOutputStream(file)){
            fos.write(bytes);
            fos.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return jsonObject;
    }
}
